package com.link.base.base.common.controller;

import com.link.base.base.common.model.Announce;
import com.link.base.base.common.service.AnnounceService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 19:06
 * @version 1.0
 */

@Controller
@RequestMapping("/link/announce")
public class AnnounceController extends BasicController<Announce> {

    @Resource
    private AnnounceService announceService;

    @Override
    public BasicService<Announce> getBasicService() throws Exception {
        return announceService;
    }

    /**
     * 查看开放的公告列表
     */
    @RequestMapping(value = "/queryPublicPage")
    @ResponseBody
    public Map<String, Object> queryPublicPage(@JsonParam QueryParams qps, @JsonParam Announce entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            Announce t = (Announce) BasicModel.transformClass(entity, qps);
            CoreUser user = UserUtil.getUser();
            Long userId = user.getId();
            List<Announce> list = announceService.queryPublicPage(t, userId);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Announce>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Announce>());
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 发布公告
     */
    @RequestMapping(value = "/publish")
    @ResponseBody
    public Map<String, Object> publish(@JsonParam Announce entity, HttpSession session,
                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            announceService.publish(entity);
            Announce newEntry = announceService.queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 撤回公告
     */
    @RequestMapping(value = "/revoke")
    @ResponseBody
    public Map<String, Object> revoke(@JsonParam Announce entity, HttpSession session,
                                      HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            announceService.revoke(entity);
            Announce newEntry = announceService.queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 修改状态
     */
    @RequestMapping(value = "/statusUpdate")
    @ResponseBody
    public Map<String, Object> statusUpdate(@JsonParam Announce entity, HttpSession session,
                                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            Long id = entity.getId();
            if (null == id) {
                throw new ServiceException("PUBLIC-007");
            }
            String anneStatus = entity.getAnneStatus();
            if (StringUtils.isBlank(anneStatus)) {
                throw new ServiceException("COMMON-001");
            }
            Announce announce = announceService.queryById(entity);
            announce.setAnneStatus(anneStatus);
            announceService.update(announce);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 修改状态
     */
    /**
     * 查看未阅读的公告列表
     */
    @RequestMapping(value = "/queryMyAnnouncePage")
    @ResponseBody
    public Map<String, Object> queryMyAnnouncePage(@JsonParam QueryParams qps,@JsonParam Announce entity, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            Announce t = (Announce) BasicModel.transformClass(entity, qps);
            CoreUser user = UserUtil.getUser();
            Long userId = user.getId();
            List<Announce> list = announceService.queryMyAnnouncePage(t, userId);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Announce>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Announce>());
            result.put("result", e.getMessage());
        }
        return result;
    }

}
